package org.mxp.demo6;

import com.rabbitmq.client.CancelCallback;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DeliverCallback;
import org.mxp.utils.RabbitMQUtils;

import java.util.Date;

public class ReceiveObj {
    public static void main(String[] args) throws Exception {
        // 交换机名称
        String EXCHANGE_NAME = "logs";
        // 获取信道
        Channel channel = RabbitMQUtils.getChannel();
        // 声明交换机，第一个参数为交换机名称，第二个参数为交换机类型
        channel.exchangeDeclare(EXCHANGE_NAME, "fanout");
        // 获取一个临时队列
        String queueName = channel.queueDeclare().getQueue();
        // 绑定临时队列于交换机，bindingkey（路由键）为空字符串
        channel.queueBind(queueName, EXCHANGE_NAME, "");
        // 获取当前时间，区分不同线程
        Date now = new Date();
        // 接受消息
        channel.basicConsume(queueName, true, (consumerTag, message) -> {
            System.out.println(now.getTime() + "接受到消息：" + new String(message.getBody()));
        }, (CancelCallback) null);
    }
}
